package services.kingdee

import com.typesafe.config.Config
import play.api.ConfigLoader

case class KingdeeConfig(serverUrl: String,
                         dCID: String,
                         lCID: Int = 2052,
                         userName: String,
                         pwd: String = "",
                         appId: String = "",
                         appSecret: String = "",
                         orgNum: String = null)

object KingdeeConfig {
  implicit val configLoader: ConfigLoader[KingdeeConfig] = new ConfigLoader[KingdeeConfig] {
    override def load(config: Config, path: String): KingdeeConfig = {
      val p = if (path.isEmpty) {
        "kingdee.k3cloud.config.default"
      } else {
        "kingdee.k3cloud.config." + path
      }
      if (config.hasPath(p)) {
        val conf = config.getConfig(p)
        KingdeeConfig(
          serverUrl = conf.getString("url"),
          dCID = conf.getString("acctID"),
          userName = conf.getString("userName"),
          pwd = conf.getString("password"),
          lCID = conf.getInt("lcid"),
          appId = conf.getString("appId"),
          appSecret = conf.getString("appSecret"),
          orgNum = conf.getString("orgNum")
        )
      } else {
        KingdeeConfig(serverUrl = "", dCID = "", userName = "")
      }
    }
  }

}
